﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SalaryChannel
{
    public enum Direction
    {
        Previous, Next
    }

    /// <summary>
    /// Function used to get data from server
    /// </summary>
    /// <param name="startIndex">The zero-based starting element position of a subsequence in this instance.</param>
    /// <param name="length">The number of element in the subsequence.</param>
    public delegate void SelectDataHandler(int startIndex, int length);

    public class SynchronousTransferPager 
    {

        /// <summary>
        /// Current scroll direction of the slidebar 
        /// </summary>
        private Direction directionValue;
        public Direction Direction
        {
            get
            {
                return directionValue;
            }
        }

        /// <summary>
        /// Current page index
        /// </summary>
        private int pageIndex;

        /// <summary>
        /// Length of current results
        /// </summary>
        private int lengthValue;

        /// <summary>
        /// Max content
        /// </summary>
        private int maxLength;

        /// <summary>
        /// Signal which shows whether it has been transferred
        /// </summary>
        private Boolean transferred = true;

        /// <summary>
        /// Delegate used to get data from server
        /// </summary>
        public SelectDataHandler SelectData
        {
            get;
            set;
        }

        public SynchronousTransferPager(int maxLength)
        {
            this.maxLength = maxLength;
        }

        /// <summary>
        /// Initialize class members.
        /// </summary>
        private void Init()
        {
            pageIndex = -1;
            lengthValue = 0;
            transferred = true;
        }

        /// <summary>
        /// Set class members.
        /// </summary>
        /// <param name="pager"></param>
        public void DataSynchronize(SynchronousTransferPager pager)
        {
            pageIndex = pager.pageIndex;
            lengthValue = pager.lengthValue;
            directionValue = pager.directionValue;
        }

        /// <summary>
        /// Judge if it is the initial page
        /// </summary>
        /// <returns></returns>
        public bool IsInitialPage()
        {
            if (pageIndex == 0 && directionValue == Direction.Next)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Judge if it is the initial page
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            if (pageIndex == -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// Judge if the next button can clicked.
        /// </summary>
        /// <returns></returns>
        public bool CanNext()
        {
            if (!transferred)
            {
                return false;
            }

            if (maxLength != lengthValue)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Judge if the previous button can clicked.
        /// </summary>
        /// <returns></returns>
        public bool CanPre()
        {
            if (transferred == false)
            {
                return false;
            }

            if (pageIndex <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Get previous page or next page data according to the direction. 
        /// </summary>
        /// <param name="direction"></param>
        public bool GetData(Direction direction)
        {
            lock (this)
            {
                if (direction == Direction.Previous)
                {
                    if (CanPre())
                    {
                        transferred = false;

                        directionValue = Direction.Previous;
                        if (SelectData != null)
                        {
                            SelectData((pageIndex - 1) * maxLength, maxLength);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    if (CanNext())
                    {
                        transferred = false;

                        directionValue = Direction.Next;
                        if (SelectData != null)
                        {
                            SelectData((pageIndex + 1) * maxLength, maxLength);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        /// <summary>
        /// Get the data of first page.
        /// </summary>
        public void BindData()
        {
            lock (this)
            {
                Init();
                transferred = false;

                directionValue = Direction.Next;
                if (SelectData != null)
                {
                    SelectData((pageIndex + 1) * maxLength, maxLength);
                }
            }
        }

        /// <summary>
        /// Transfer done with some result.
        /// </summary>
        /// <param name="length">The length of results</param>
        public void GetDataCompleted(int length)
        {
            lock (this)
            {
                transferred = true;
                this.lengthValue = length;

                if (directionValue == Direction.Previous)
                {
                    if (length < maxLength && length > 0)
                    {
                        pageIndex = 0;
                    }
                    else if (length == 0)
                    {
                        pageIndex = -1;
                    }
                    else if(length==maxLength)
                    {
                        pageIndex--;
                    }
                }
                else
                {
                    if (length != 0)
                    {
                        pageIndex++;
                    }
                }
            }
        }
    }
}
